package com.monkey.controller;

import com.monkey.core.ErrorCode;
import com.monkey.core.WebRet;
import com.monkey.model.admin.AdminLoginData;
import com.monkey.model.admin.AdminToken;
import com.monkey.model.reward.RewardData;
import com.monkey.model.reward.RewardListData;
import com.monkey.model.user.User;
import com.monkey.model.user.UserDrawData;
import com.monkey.service.AdminService;
import com.monkey.service.RewardService;
import com.monkey.service.UserService;
import com.monkey.util.HelpUtil;
import com.monkey.util.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.monkey.service.AdminService.DRAW_ALL;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2018/1/16.
 */
@RestController
@RequestMapping(value = "/admin")
public class AdminController {
    private static Logger logger = LoggerFactory.getLogger(AdminController.class);

    @Resource
    private AdminService adminService = null;

    @Resource
    private UserService userService = null;

    @Resource
    private RewardService rewardService = null;

    @Resource
    private HelpUtil helpUtil = null;

    @Resource
    private WebConfig webConfig = null;

    /**
     * 后台管理系统登录
     *
     * @param account  string 账号
     * @param password string 密码
     * @return token 成功的话返回
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login_log(@RequestBody Map<String, Object> params, @RequestHeader String key) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        String account = (String) params.get("account");
        String password = (String) params.get("password");
        if (null == key || !key.equals(key1)) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        if (null == account || account.isEmpty() || null == password || password.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        AdminLoginData data = adminService.login(account, password);
        if (ErrorCode.SUCCESS == data.getCode()) {
            ret.put("code", data.getCode());
            ret.put("msg", data.getMsg());
            ret.put("token", data.getToken());
            ret.put("admin", data.getAdmin());
        } else {
            ret.put("code", data.getCode());
            ret.put("msg", data.getMsg());
        }
        return ret;
    }

    /**
     * 后台管理系统登出
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public Map<String, Object> logout_log(@RequestBody Map<String, Object> params, @RequestHeader String key, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1) || null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        AdminToken admin = adminService.getAdminWithToken(token);
        if (null == admin) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        adminService.deleteToken(token);
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        return ret;
    }

    /**
     * key: 发送奖励
     *
     * @param value string 奖励物品，
     *              奖励宠物 手机号，类型，宠物id，代数，品种，套装，成长，生育，掘金
     *              136xxxxxxxx,1,88,0,1,1,1.8,1.9,1.5
     *              奖励金币 手机号，类型，金币数
     *              126xxxxxxxx,2,9999
     */
    @RequestMapping(value = "/sendReward", method = RequestMethod.POST)
    public Map<String, Object> sendReward_log(@RequestBody Map<String, Object> params, @RequestHeader String key, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1) || null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        AdminToken admin = adminService.getAdminWithToken(token);
        if (null == admin) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        String value = (String) params.get("value");
        if (null == value || value.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        RewardData ret1 = rewardService.sendReward(value);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * key: 获取奖励列表
     *
     * @param offset int 偏移量
     * @param num    int 数量
     */
    @RequestMapping(value = "/getRewardList", method = RequestMethod.POST)
    public Map<String, Object> getRewardList_log(@RequestBody Map<String, Object> params, @RequestHeader String key, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1) || null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        AdminToken admin = adminService.getAdminWithToken(token);
        if (null == admin) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int offset = (int) params.get("offset");
        int num = (int) params.get("num");
        if (0 > offset || 0 > num) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }
        offset = (offset - 1) * num;
        RewardListData ret1 = rewardService.getRewardList(offset, num);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        ret.put("total", ret1.getTotal());
        ret.put("list", ret1.getList());
        return ret;
    }

    /**
     * 提现请求接口
     *
     * @param price string 提现金额
     */
    @RequestMapping(value = "/drawMoney", method = RequestMethod.POST)
    public Map<String, Object> drawMoney_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        String price = (String) params.get("price");
        if (null == price || price.isEmpty()) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }

        WebRet ret1 = adminService.drawMoney(user.getId(), Float.valueOf(price));
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * key: 提现完成接口，只对后台系统开放
     *
     * @param id int 玩家id
     */
    @RequestMapping(value = "/drawMoneyComplete", method = RequestMethod.POST)
    public Map<String, Object> drawMoneyComplete_log(@RequestBody Map<String, Object> params, @RequestHeader String key, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1) || null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        AdminToken admin = adminService.getAdminWithToken(token);
        if (null == admin) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int id = (int) params.get("id");
        User user = userService.getUser(id);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        WebRet ret1 = adminService.drawMoneyComplete(user.getId());
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * 提现取消接口，暂时不用对外开放
     */
    @RequestMapping(value = "/drawMoneyCancel", method = RequestMethod.POST)
    public Map<String, Object> drawMoneyCancel_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        WebRet ret1 = adminService.drawMoneyCancel(user.getId());
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * key: 获取提现列表
     *
     * @param offset int 偏移量
     * @param num    int 数量
     * @param status int 状态 申请0，完成1，取消2
     */
    @RequestMapping(value = "/getDrawList", method = RequestMethod.POST)
    public Map<String, Object> getDrawList_log(@RequestBody Map<String, Object> params, @RequestHeader String key, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1) || null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        AdminToken admin = adminService.getAdminWithToken(token);
        if (null == admin) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int offset = (int) params.get("offset");
        int num = (int) params.get("num");
        int status = (int) params.getOrDefault("status", DRAW_ALL);
        if (0 > offset || 0 > num) {
            ret.put("code", ErrorCode.PARAM_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.PARAM_ERROR));
            return ret;
        }
        offset = (offset - 1) * num;
        UserDrawData ret1 = adminService.getDrawList(offset, num, status);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        ret.put("total", ret1.getTotal());
        ret.put("list", ret1.getList());
        return ret;
    }

    /**
     * key: 服务器开放开关(内部接口不对外开放)
     */
    @RequestMapping(value = "/allow", method = RequestMethod.POST)
    public Map<String, Object> allow(HttpServletRequest request, @RequestHeader String key, @RequestHeader int allow) {
        Map<String, Object> ret = new HashMap<>();
        // 拦截非本地的访问
        String ip = helpUtil.getIp(request);
        logger.info("!!! allow login:{}, {} !!!", ip, (0 != allow));
        if (!ip.equals("127.0.0.1") && !ip.equals("0:0:0:0:0:0:0:1")) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1)) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        WebRet ret1 = adminService.setAllow(0 != allow);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }

    /**
     * key: 优雅关闭服务器(内部接口不对外开放)
     */
    @RequestMapping(value = "/shutdown", method = RequestMethod.POST)
    public Map<String, Object> shutdown(HttpServletRequest request, @RequestHeader String key) {
        Map<String, Object> ret = new HashMap<>();
        // 拦截非本地的访问
        String ip = helpUtil.getIp(request);
        logger.info("!!! shutdown:{} !!!", ip);
        if (!ip.equals("127.0.0.1") && !ip.equals("0:0:0:0:0:0:0:1")) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        String key1 = webConfig.get("system.admin.key");
        if (null == key || !key.equals(key1)) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }

        WebRet ret1 = adminService.shutdown();
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        return ret;
    }
}
